from django.views.generic import ListView

from core.backend_views import BackendListBaseView
from expense.models import PayCategory, Expense
from system.models import Iconfont
from utils.paginator import PaginationMixin, LoverViewMixin
from utils.utils import get_queryset_default_dict


class PayCategoryView(PaginationMixin, ListView):
    model = PayCategory
    context_object_name = "paycategories"
    template_name = "backend/expense/category.html"

    def get_context_data(self, *, object_list=None, **kwargs):
        kwargs['pay_categories'] = get_queryset_default_dict(
            PayCategory.objects.filter().filter_lover(self.request.user).order_by('pay_type', 'sort').values(),
            'pay_type')
        kwargs['ali_icons'] = Iconfont.objects.filter().values('name', 'font_class')

        return super(PayCategoryView, self).get_context_data(object_list=object_list, **kwargs)


class ExpenseView(LoverViewMixin, PaginationMixin, BackendListBaseView):
    model = Expense
    context_object_name = "expenses"
    template_name = "backend/expense/expense.html"
    page_name = '账单列表'

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.select_related('created_by')

    def get_context_data(self, *, object_list=None, **kwargs):
        kwargs['pay_categories'] = get_queryset_default_dict(
            PayCategory.objects.filter().filter_lover(self.request.user).order_by('pay_type', 'sort').values(),
            'pay_type')
        return super().get_context_data(object_list=object_list, **kwargs)
